def createCounter():
    i = 0
    def counter():
        nonlocal i
        i= i +1
        return i
    return counter

# 测试:
counterA = createCounter()
print(counterA(), counterA(), counterA(), counterA(), counterA()) # 1 2 3 4 5
counterB = createCounter()
if [counterB(), counterB(), counterB(), counterB()] == [1, 2, 3, 4]:
    print('测试通过!')
else:
    print('测试失败!')


def is_odd(n):
    return n % 2 == 1

L = list(filter(lambda x: x%2 ==1, range(1, 20)))

print(L)


def log(func):
    def wrapper(*args, **kw):
        print('call %s():' % func.__name__)
        return func(*args, **kw)
    return wrapper

@log
def now():
    print('2024-6-1')

now()




import time, functools,datetime

def metric1(tag_or_fn):
    if callable(tag_or_fn):
        @functools.wraps(tag_or_fn)
        def wrapper(*args, **kw):
            print('%s %s %s start execut ' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") , "default", tag_or_fn.__name__))
            t =  tag_or_fn(*args, **kw)
            print('%s %s %s execut completed ' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") , "default", tag_or_fn.__name__))
            return t
        return wrapper
    else:
        def decorator(fn):
                @functools.wraps(fn)
                def wrapper(*args, **kw):
                    print('%s %s  %s start execut ' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") ,tag_or_fn,  fn.__name__))
                    t =  fn(*args, **kw)
                    print('%s %s  %s execut completed ' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") , tag_or_fn, fn.__name__))
                    return t
                return wrapper
        return decorator

def metric(fn):
    @functools.wraps(fn)
    def wrapper(*args, **kw):
        print('%s %s start execut ' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") ,  fn.__name__))
        t=  fn(*args, **kw)
        print('%s %s execut completed ' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") ,  fn.__name__))
        return t
    return wrapper



# 测试
@metric1
def fast(x, y):
    print("fast")
    time.sleep(0.0012)
    return x + y;

@metric1("tt")
def slow(x, y, z):
    time.sleep(0.1234)
    return x * y * z;

f = fast(11, 22)

print(f"f={f}")
s = slow(11, 22, 33)
print(f"s={s}")
if f != 33:
    print('测试失败!')
elif s != 7986:
    print('测试失败!')

fast2 = functools.partial(fast,y = 10)

f = fast2(1331)

print(f"f={f}")



class Student(object):
    count = 0

    def __init__(self, name):
    #   nonlocal count
      self.name = name
      Student.count += 1
        

# 测试:
if Student.count != 0:
    print('测试失败1!')
else:
    bart = Student('Bart')
    if Student.count != 1:
        print(f'测试失败2! :{Student.count}')
    else:
        lisa = Student('Bart')
        if Student.count != 2:
            print('测试失败3!')
        else:
            print('Students:', Student.count)
            print('测试通过!')

# width和height属性，以及一个只读属性resolution
class Screen(object):



    @property
    def width(self):
        return self._width
    
    @width.setter
    def width(self,value):
        self._width  = value

    @property
    def height(self):
        return self._height
    
    @height.setter
    def height(self,value):
        self._height = value

    @property
    def resolution(self):
        return  self._width * self._height

# 测试:
s = Screen()
s.width = 1024
s.height = 768
print('resolution =', s.resolution)
if s.resolution == 786432:
    print('测试通过!')
else:
    print('测试失败!')


class Student1(object):
    def __init__(self, name):
        self.name = name
    def __str__(self):
       return 'Student object (name: %s)' % self.name

s = Student1("tangdekun")

s

print(s)


from enum import Enum

Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))

print(Month.Jan)

from enum import Enum, unique

@unique
class Gender(Enum):
    Male = 0
    Female = 1

class Student(object):
    def __init__(self, name, gender):
        self.name = name
        self.gender = gender

# 测试:
bart = Student('Bart', Gender.Male)
if bart.gender == Gender.Male:
    print('测试通过!')
else:
    print('测试失败!')